package com.shanghai.market.plshnaghai.PopWindows;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanghai.market.plshnaghai.R;
import com.shanghai.market.plshnaghai.constants.constant;
import com.shanghai.market.plshnaghai.publics.LogCat;
import com.shanghai.market.plshnaghai.utils.ToastUtils;

import java.io.File;

/**
 * 头部肖像窗口
 * 
 * @author Administrator
 *
 */
public class HeadPortraitPopWindow implements OnClickListener {

	private Activity mActivity;
	private PopupWindow mPopupWindow;
	private View mView;
	private TextView mTextPhotograph;
	private TextView mTextPhotoAlbum;
	private TextView mTextCancel;
	private String imagePath;

	@SuppressLint("InflateParams")
	public HeadPortraitPopWindow(Activity activity) {
		this.mActivity = activity;
		final LayoutInflater inflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.popup_window_head_portrait, null);
	}

	public String getImagePath() {
		return imagePath;
	}

	public void initPopWindow(String filePath) {
		if (mPopupWindow != null) {
			return;
		}

		mPopupWindow = new PopupWindow(mView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
				true);
//		mPopupWindow.setAnimationStyle(R.style.HeadPortraitAnimStyle);
		mTextPhotograph = (TextView) mView.findViewById(R.id.text_photograph);
		mTextPhotoAlbum = (TextView) mView.findViewById(R.id.text_photo_album);
		mTextCancel = (TextView) mView.findViewById(R.id.text_cancel);
		WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
		params.alpha = 0.75f;
		mTextPhotograph.setOnClickListener(this);
		mTextPhotoAlbum.setOnClickListener(this);
		mActivity.getWindow().setAttributes(params);

		initListeners();
	}

	public void showAsDropDown(View parent, int xoff, int yoff) {
		mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, xoff, yoff);
		LogCat.i("mView:" + mView.getHeight());
	}

	public void closePopupWindow() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
			WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
			params.alpha = 1f;
			mActivity.getWindow().setAttributes(params);
		}
	}

	@SuppressLint("ClickableViewAccessibility")
	private void initListeners() {
		mView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				closePopupWindow();
				return false;
			}
		});

		mTextCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				closePopupWindow();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.text_photograph:
			takePhotos();
			closePopupWindow();
			break;
		case R.id.text_photo_album:
			getSystemImg();
			closePopupWindow();
			break;
		case R.id.text_cancel:
			closePopupWindow();
			break;
		}

	}

	private void takePhotos() {
		getImageFromCamera();
		
//		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//		mActivity.startActivityForResult(intent, Constant.RESULT_PIC);
	}

	private void getSystemImg() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image/*");
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
			mActivity.startActivityForResult(intent, constant.SELECT_PIC_KITKAT);
		} else {
			mActivity.startActivityForResult(intent, constant.SELECT_PIC);
		}
		
//		Intent intent = new Intent(Intent.ACTION_PICK,
//				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//		mActivity.startActivityForResult(intent, Constant.RESULT_SYSTEM_PIC);
	}
	
	private void getImageFromCamera() {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
			String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
					+ constant.CAMERA_DIR_PATH;
			File dir = new File(filePath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			
			imagePath = filePath + System.currentTimeMillis() + ".jpg";
			intent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(imagePath)));
			intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
			intent.putExtra("imagePath", imagePath);
			mActivity.startActivityForResult(intent, constant.SELECT_CAMERA);
		} else {
			ToastUtils.show("请确认已经插入SD卡");
		}
	}
}
